import Ember from 'ember';
import Changeset from 'ember-changeset';
import CustomerbusinesstryValidations from '../../../validations/customerbusinesstry';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(CustomerbusinesstryValidations,{
  constants:Constants,
  store: Ember.inject.service('store'),
  dataLoader: Ember.inject.service("data-loader"),
  dateService: Ember.inject.service("date-service"),
  defaultStaff:Ember.computed('customerbusinessflowInfo.experienceStaff',function(){
      return this.get('customerbusinessflowInfo.experienceStaff');
  }),
  defaultBed:Ember.computed('customerbusinessflowInfo.orderBed','customerbusinessflowInfo.experienceBed','firstBed',function(){
    if(this.get('customerbusinessflowInfo.experienceBed').content){
      return this.get('customerbusinessflowInfo.experienceBed');
    }else{
      return this.get('customerbusinessflowInfo.orderBed');
    }

  }),
  referencePrice:Ember.computed('customerbusinessflowModel.@each.experienceBed','customerbusinessflowModel.@each.diningStandard','project',function(){
    let bedPrice;
    let diningPrice;
    let projectPrice;
    if(this.get('customerbusinessflowModel.experienceBed.price')){
      bedPrice = parseInt(this.get('customerbusinessflowModel.experienceBed.price'));
    }else{
      bedPrice = parseInt(this.get('customerbusinessflowModel.orderBed.price'));
    }
    if(this.get('customerbusinessflowModel.diningStandard.remark')){
      diningPrice = parseInt(this.get('customerbusinessflowModel.diningStandard.remark'));
    }else{
      diningPrice = 0;
    }
    if(this.get('project.price')){
      projectPrice = parseInt(this.get('project.price'));
    }else{
      projectPrice = 0;
    }

    let price = bedPrice+diningPrice+projectPrice;
    return price ;

  }),
  priceObs:function(){
    let bedPrice;
    let diningPrice;
    let projectPrice;
    if(this.get('customerbusinessflowModel.experienceBed.price')){
      bedPrice = parseInt(this.get('customerbusinessflowModel.experienceBed.price'));
    }else{
      bedPrice = parseInt(this.get('customerbusinessflowModel.orderBed.price'));
    }
    if(this.get('customerbusinessflowModel.diningStandard.remark')){
      diningPrice = parseInt(this.get('customerbusinessflowModel.diningStandard.remark'));
    }else{
      diningPrice = 0;
    }
    if(this.get('project.price')){
      projectPrice = parseInt(this.get('project.price'));
    }else{
      projectPrice = 0;
    }

    let price = bedPrice+diningPrice+projectPrice;

    this.set('referencePrice',price);
  }.observes('customerbusinessflowModel.diningStandard','customerbusinessflowModel.experienceBed'),
  customerbusinessflowModel:Ember.computed('customerbusinessflowInfo',function(){
    let model = this.get("customerbusinessflowInfo");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(CustomerbusinesstryValidations), CustomerbusinesstryValidations);
  }),
  actions:{
    invalid() {
        //alert("error");
    },
    toProject(project){
      let id = project.get("id");
      let mainpageController = App.lookup('controller:business.mainpage');
      mainpageController.switchMainPage('nursingproject-detail',{editMode:'edit',id:id});
    },
    //暂存
    stagingAction(){
      let customerbusinessflowInfo = this.get('customerbusinessflowInfo');
      let status = this.get("dataLoader").findDict("consultStatus4");
      let _self = this;
      if(!_self.get('customerbusinessflowInfo.experienceBed').content){
        customerbusinessflowInfo.set('experienceBed',_self.get('customerbusinessflowInfo.orderBed'));
      }
      let curBed = _self.get('store').peekRecord('bed',customerbusinessflowInfo.get('experienceBed.id'));
      let bedStatus = _self.get("dataLoader").findDict("bedStatusTryIn");
      if(curBed){
        curBed.set('status',bedStatus);
        curBed.save();
      }
      customerbusinessflowInfo.set('tempFlag',1);
      customerbusinessflowInfo.save().then(function(){
        App.lookup('controller:business.mainpage').openPopTip("正在保存");
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('business-customer');
        App.lookup('controller:business.mainpage').showPopTip("保存成功");
      });
    },
    //保存
    detailSaveClick: function() {
      let editMode=this.get('editMode');
      let customerbusinessflowModel=this.get('customerbusinessflowModel');
      let customerbusinessflowInfo = this.get('customerbusinessflowInfo');
      let _self=this;
      let status = this.get("dataLoader").findDict("consultStatus4");
      if(!_self.get('customerbusinessflowInfo.experienceBed').content){
        customerbusinessflowInfo.set('experienceBed',_self.get('customerbusinessflowInfo.orderBed'));
      }
      if(this.get('project.price')){
        customerbusinessflowModel.validate().then(function(){
          let curBed = _self.get('store').peekRecord('bed',customerbusinessflowInfo.get('experienceBed.id'));
          let bedStatus = _self.get("dataLoader").findDict("bedStatusTryIn");
          curBed.set('status',bedStatus);
          curBed.save().then(function(){
            let curCustomer = _self.get('store').peekRecord('customer',customerbusinessflowInfo.get('customer.id'));
            let customerStatus = _self.get("dataLoader").findDict("customerStatusTry");
            curCustomer.set('customerStatus',customerStatus);
            curCustomer.set('diningStandard',customerbusinessflowModel.get('diningStandard'));
            curCustomer.set('bed',curBed);
            curCustomer.save();
            customerbusinessflowInfo.set('status',status);
            customerbusinessflowInfo.set('tempFlag',0);
            customerbusinessflowInfo.set('experienceRemark',_self.get('customerbusinessflowInfo.experienceRemark'));
            customerbusinessflowInfo.set('experienceEndTime',customerbusinessflowModel.get('experienceEndTime'));
            customerbusinessflowInfo.set('experienceStartTime',customerbusinessflowModel.get('experienceStartTime'));
            customerbusinessflowInfo.set('experienceDate',customerbusinessflowModel.get('experienceDate'));
            customerbusinessflowInfo.set('experiencePrice',customerbusinessflowModel.get('experiencePrice'));
            customerbusinessflowInfo.set('diningStandard',customerbusinessflowModel.get('diningStandard'));
            if(customerbusinessflowModel.get('errors.length')===0){
              App.lookup('controller:business.mainpage').openPopTip("正在保存");
              customerbusinessflowInfo.save().then(function(){
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                if(editMode=='add'){
                  let mainpageController = App.lookup('controller:business.mainpage');
                  mainpageController.switchMainPage('business-customer');
                }
          });
          }else{
            customerbusinessflowModel.set("validFlag",Math.random());
          }
          });
        });
      }else{
        alert('请填写评估问卷');
      }


    },
    //编辑按钮
    detailEditClick:function(){
      this.set('detailEdit',true);
    },
    //取消
    detailCancel:function(){
      let id=this.get('id');
      let editMode=this.get('editMode');
      if(id&&editMode=='edit'){
        this.set('detailEdit',false);
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('business-customer');
      }else{
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('business-customer');
      }
    },
    //选择床位
    selectBed(Bed) {
        this.set('customerbusinessflowInfo.experienceBed',Bed);
        this.set('customerbusinessflowModel.experienceBed',Bed);
    },
    //选择办理人
    selectStaff(staff){
      this.set('customerbusinessflowInfo.experienceStaff',staff);
      this.set('customerbusinessflowModel.experienceStaff',staff);
    },
    //选择入院日期
    changeTryEndAction(date){
      //当前选择时间
      let stamp = this.get("dateService").timeStringToTimestamp(date);
      this.set("customerbusinessflowModel.experienceEndTime", stamp);
    },
    changeTryStartAction(date){
      //当前选择时间
      let stamp = this.get("dateService").timeStringToTimestamp(date);
      this.set("customerbusinessflowModel.experienceStartTime", stamp);
    },
    //选择预定办理日期
    changeTryAction(date){
      let stamp = this.get("dateService").timeStringToTimestamp(date);
      console.log("stamp",stamp);
      this.set("customerbusinessflowModel.experienceDate", stamp);
    },
    dpShowAction(){},

  }
});
